package com.simtop.controller;

import com.simtop.common.ServerResponse;
import com.simtop.pojo.ModelData;
import com.simtop.pojo.Momo;
import com.simtop.pojo.User;
import com.simtop.util.JwtUtil;
import com.simtop.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author lvyongwei
 * 前端U3D交互
 */
@Controller
//@CrossOrigin
@RequestMapping("/unity")
public class U3dController {

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 记录场景信息
     * @param modelData
     * @param request
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public ServerResponse add(@RequestBody ModelData modelData, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try{
            redisUtil.set(u.getId() +":"+ u.getMillTime() + ":" + modelData.getId(),modelData,10*60*60*1000L);
            return ServerResponse.createBySuccessMsg("提交内容成功");
        } catch (Exception e){
            return ServerResponse.createByErrorMsg("提交内容失败");
        }
    }

    /**
     * 张震
     * @param
     * @param
     * @return
     */
    @PostMapping("/addUmodel")
    @ResponseBody
    public ServerResponse addUmodel(@RequestBody Momo momo, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try{
            redisUtil.set(u.getId() +":"+ u.getMillTime(),momo,10*60*60*1000L);
            return ServerResponse.createBySuccessMsg("提交内容成功");
        } catch (Exception e){
            return ServerResponse.createByErrorMsg("提交内容失败");
        }
    }

    /**
     * 记录场景信息
     * @param request
     * @return
     */
    @PostMapping("/refresh")
    @ResponseBody
    public ServerResponse refresh(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try{
            ModelData modelData = new ModelData(1, 0, 0);
            ModelData modelData1 = new ModelData(2, 0, 0);
            redisUtil.set(u.getId() +":"+ u.getMillTime() + ":" + 1,modelData,10*60*60*1000L);
            redisUtil.set(u.getId() +":"+ u.getMillTime() + ":" + 2,modelData1,10*60*60*1000L);
            return ServerResponse.createBySuccessMsg("提交内容成功");
        } catch (Exception e){
            return ServerResponse.createByErrorMsg("提交内容失败");
        }
    }


    /**
     * 获取场景信息
     * @param modelData
     * @param request
     * @return
     */
    @PostMapping("/get")
    @ResponseBody
    public ServerResponse get(@RequestBody ModelData modelData, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try{
            ModelData data = (ModelData) redisUtil.get(u.getId() +":"+ u.getMillTime() + ":" + modelData.getId());
            return ServerResponse.createBySuccess("获取内容成功",data);
        } catch (Exception e){
            return ServerResponse.createByErrorMsg("获取内容失败");
        }
    }

    @PostMapping("/getMd")
    @ResponseBody
    public ServerResponse getMd( HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try{
            Momo data = (Momo) redisUtil.get(u.getId() +":"+ u.getMillTime());
            return ServerResponse.createBySuccess("获取内容成功",data);
        } catch (Exception e){
            return ServerResponse.createByErrorMsg("获取内容失败");
        }
    }
}
